<?php

namespace Product\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Library\String;


class ProductController extends AbstractActionController{
    
    private $objView;
    
    private $productModel;


    public function __construct() {
        
        $this->objView  = new ViewModel();
        
        $session = new \Zend\Session\Container('member');
        if($session->offsetExists('member_email')){
            $this->objView->setVariable('member_email', $session->offsetGet('member_email'));
        }
    }
    
    public function _setCategory() {
        
        $aryCategory = $this->getProductModel()->getProductCategory();
        
        $this->objView->setVariable('aryCategory', $aryCategory);
    }
    
    public function _setMaker($categoryId) {
        
        $aryMaker = $this->getProductModel()->getMaker($categoryId);
        
        $this->objView->setVariable('aryMaker', $aryMaker);
    }
    
    public function searchAction() {
        
        $this->_setCategory();
        
        $keyword = $this->params()->fromRoute('keyword');
        
        $page = $this->params()->fromRoute('page');
        
        $stripTags = new \Zend\Filter\StripTags();
        
        $filterKeyword = $stripTags->filter($keyword);
        
        $aryConditions = array();
        
        $aryConditions['encode_keyword'] = String::encodingUnicode($filterKeyword);
        
        $aryConditions['keyword'] = $filterKeyword;
        
        $aryConditions['current_page'] = $page;
        
        $config = $this->getServiceLocator()->get('config');
        
        $aryConditions['record_per_page'] = $config['product_limit']['product_list'];
        
        //print_r($aryConditions);
        
        $aryProduct = $this->getProductModel()->searchProduct($aryConditions);
        
        $this->objView->setVariables($aryConditions);
        
        $this->objView->setVariable('aryProduct', $aryProduct);
        
        $this->objView->setTemplate('product/_product-search');
        
        return $this->objView;
    }
    
    public function indexAction() {

        $config = $this->getServiceLocator()->get('config');
        
        $limit = $config['product_limit']['product_by_type'];
        
        $aryNewProduct = $this->getProductModel()->getProductByType(1, $limit);
        
        $arySaleOfProduct = $this->getProductModel()->getProductByType(2, $limit);
        
        $aryTopProduct = $this->getProductModel()->getProductByType(3, $limit);
        
        $this->_setCategory();
        
        $this->objView->setVariable('aryNewProduct', $aryNewProduct);
        
        $this->objView->setVariable('arySaleOfProduct', $arySaleOfProduct);
        
        $this->objView->setVariable('aryTopProduct', $aryTopProduct);
        
        $this->objView->setTemplate('product/_home');
        
        return $this->objView;
    }
    
    public function listProductByTypeAction() {
        
        $this->_setCategory();
        
        $type = $this->params()->fromRoute('type');
        
        $config = $this->getServiceLocator()->get('config');
        
        $typeName = $config['product_type'][$type];
        
        $limit = $config['product_limit']['product_list'];
        
        $aryProduct = $this->getProductModel()->getProductByType($type, $limit);
        
        $this->objView->setVariable('aryProduct', $aryProduct);
        
        $this->objView->setVariable('typeName', $typeName);
        
        $this->objView->setTemplate('product/_product-by-type');
        
        return $this->objView;
    }
    
    public function productDetailAction() {
        
        $this->_setCategory();
        
        $productId = $this->params()->fromRoute('product_id');
        
        $aryProduct = $this->getProductModel()->getProductDetail($productId);
 
        $this->objView->setVariable('aryProduct', $aryProduct[0]);
        
        $this->objView->setTemplate('product/_product_detail');
        
        return $this->objView;
    }
    
    public function listProductByCategoryAction() {
        
        $categoryId = $this->params()->fromRoute('cat_id');
        
        $this->_setMaker($categoryId);
        
        $this->_setCategory();
        
        $this->_setPriceStep();
        
        $config = $this->getServiceLocator()->get('config');
        $aryConditions = array();
        $aryConditions['category_id'] = $categoryId;
        $aryConditions['current_page'] = $this->params()->fromRoute('page');
        $aryConditions['limit'] = $config['product_limit']['product_list'];
        $aryConditions['category_filter'] = str_replace('.html', '', $this->params()->fromRoute('cat_name'));
        $aryConditions['order'] = $config['order_by'][$this->params()->fromRoute('order_by')];
        $aryConditions['order_by'] = $this->params()->fromRoute('order_by');
        $aryConditions['maker_name'] = $this->params()->fromRoute('maker_name');
        $aryConditions['price_step'] = $this->params()->fromRoute('price_step');
        
        $aryPrice = explode('-', $aryConditions['price_step']);
        
        $aryConditions['price_from'] = (int)trim($aryPrice[0]);
        $aryConditions['price_to'] = (int)trim($aryPrice[1]);
        
        $aryProduct = $this->getProductModel()->getListProduct($aryConditions);
        
        $this->objView->setVariables($aryConditions);
        
        $this->objView->setVariable('aryProduct', $aryProduct);
        
        $this->objView->setTemplate('product/_product-by-category');
        
        return $this->objView;
    }
    
    public function _setPriceStep() {
        $price = array();
        $price[0]['string'] = '< 1.Tr';
        $price[0]['step'] = '0-1000000';
        $price[1]['string'] = '1.Tr - 3.Tr';
        $price[1]['step'] = '1000000-3000000';
        $price[2]['string'] = '3.Tr - 5.Tr';
        $price[2]['step'] = '3000000-5000000';
        $price[3]['string'] = '5.Tr - 7.Tr';
        $price[3]['step'] = '5000000-7000000';
        $price[4]['string'] = '7.Tr - 10.Tr';
        $price[4]['step'] = '7000000-10000000';
        $price[5]['string'] = '10.Tr - 15.Tr';
        $price[5]['step'] = '10000000-15000000';
        $price[6]['string'] = '15.Tr - 20.Tr';
        $price[6]['step'] = '15000000-20000000';
        $price[7]['string'] = '20.Tr - 30.Tr';
        $price[7]['step'] = '20000000-30000000';
        $price[8]['string'] = '30.Tr - 40.Tr';
        $price[8]['step'] = '30000000-40000000';
        $price[9]['string'] = '40.Tr - 50.Tr';
        $price[9]['step'] = '40000000-50000000';
        $price[10]['string'] = '50.Tr - 60.Tr';
        $price[10]['step'] = '50000000-60000000';
        $price[11]['string'] = '60.Tr - 80.Tr';
        $price[11]['step'] = '60000000-80000000';
        $price[12]['string'] = '80.Tr - 100.Tr';
        $price[12]['step'] = '80000000-100000000';
        
        $this->objView->setVariable('aryPriceStep', $price);
    }
    
    public function getProductModel() {
        
        if(!$this->productModel){
            $this->productModel = $this->getServiceLocator()->get('ProductModel');
        }
        return $this->productModel;
    }
    
}
